package lch.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import lch.dao.ActiveMainDao;
import lch.pojo.Menu;
import lch.service.ActiveMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author lch
 * @date 2020-01-19 18:01
 */
@Service(interfaceClass = ActiveMainService.class)
@Transactional
public class ActiveMainServiceImpl implements ActiveMainService {
    @Autowired
    private ActiveMainDao activeMainDao;

    @Override
    public List<Menu> menu(String username) {
        List<Menu> menuList = activeMainDao.menu(username);//获取21条数据
        List<Menu> list = new ArrayList<>();//封装5个主菜单，工作台数据库没有
        for (Menu menu : menuList) {
            if (StringUtils.isEmpty(menu.getParentMenuId())) {
                //menu为父目录
                list.add(menu);
            } else {
                //menu为子目录
                for (Menu menu1 : list) {
                    //判断外循环菜单的ParentMenuId和内循环菜单的id，相等就加入对应id主菜单
                    if (menu.getParentMenuId().equals(menu1.getId())) {
                        List<Menu> children = menu1.getChildren();
                        children.add(menu);
                    }
                }
            }
        }
        return list;
    }
}